package com.yy.job.boot.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.RandomUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Lists;
import com.yy.job.boot.service.Case1Service;
import com.yy.job.boot.service.Case2Service;
import com.yy.job.boot.service.Case3Service;
import com.yy.job.boot.utils.RequestIPUtils;
import com.yy.job.common.utils.BaseResult;
import com.yy.job.common.utils.date.YYDateUtils;

@Controller
@RequestMapping("/boot/job")
public class JobBootController {

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private Case1Service case1Service;

	@Autowired
	private Case3Service case3Service;

	@Autowired
	private Case2Service case2Service;

	private Date lastDate = null;

	/**
	 * 创建单步骤且成功的任务
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/createTrue.json", method = RequestMethod.GET)
	@ResponseBody
	public BaseResult<Long> createTrue(HttpServletRequest request, HttpServletResponse response) throws Exception {
		logger.info("createTrue");

		// 创建job
		Long create = case1Service.createTrue(request, null);

		return BaseResult.success(create);
	}

	/**
	 * 创建单步骤且失败的任务
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/createFalse.json", method = RequestMethod.GET)
	@ResponseBody
	public BaseResult<Long> createFalse(HttpServletRequest request, HttpServletResponse response) throws Exception {
		logger.info("createFalse");

		// 创建job
		Long create = case1Service.createFalse(request, null);

		return BaseResult.success(create);
	}

	/**
	 * 创建延迟任务
	 *
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/createExpected.json", method = RequestMethod.GET)
	@ResponseBody
	public BaseResult<Long> createExpected(HttpServletRequest request, HttpServletResponse response) throws Exception {
		logger.info("createExpected");

		// 创建job
		Long create = case1Service.createExpected(request, null);

		return BaseResult.success(create);
	}

	/**
	 * 创建本地执行的任务。分布式的情况下，可以保证本机创建本机执行
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/createLocal.json", method = RequestMethod.GET)
	@ResponseBody
	public BaseResult<Long> createLocal(HttpServletRequest request, HttpServletResponse response) throws Exception {
		logger.info("createLocal");

		// 创建job
		Long create = case3Service.createLocal(request, null);

		return BaseResult.success(create);
	}

	/**
	 * 创建高优先级任务。可以插队执行。
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/createPriority.json", method = RequestMethod.GET)
	@ResponseBody
	public BaseResult<Long> createPriority(HttpServletRequest request, HttpServletResponse response) throws Exception {
		logger.info("createPriority");

		// 创建job
		Long create = case3Service.createPriority(request, null);

		return BaseResult.success(create);
	}

	/**
	 * 随机创建100个高优先及执行任务
	 *
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/create100Priorityjobs.json", method = RequestMethod.GET)
	@ResponseBody
	public BaseResult<String> create100Priorityjobs(HttpServletRequest request, HttpServletResponse response) throws Exception {
		logger.info("create100jobs");
		
		Date now = new Date();
		if (lastDate != null) {
			int intervalMinute = YYDateUtils.getIntervalMinute(lastDate, now);
			if (intervalMinute < 30) {
				return BaseResult.fail("20001", "每30分钟可以点击一次");
			}
		}
		lastDate = now;
		String ipAddress = RequestIPUtils.getIPAddress(request);
		int totalCount = RandomUtils.nextInt(200,1000);
		
		new Thread(() -> {
			try {
				List<Long> ids = Lists.newArrayList();
				for (int i = 0; i < totalCount; i++) {
					int nextInt = RandomUtils.nextInt(0, 7);
					System.out.println("========" + nextInt);

					// 创建job
					Long create = case3Service.createPriority(request, ipAddress);
					ids.add(create);
				}
			} catch (Exception ex) {
				logger.error("create job wrong", ex);
			}
		}).start();

		return BaseResult.success("异步生成，请在yy-job控制台刷新查看任务执行情况");
	}

	/**
	 * 随机创建100个任务
	 *
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/create100jobs.json", method = RequestMethod.GET)
	@ResponseBody
	public BaseResult<String> create100jobs(HttpServletRequest request, HttpServletResponse response) throws Exception {
		logger.info("create100jobs");
		
		Date now = new Date();
		if (lastDate != null) {
			int intervalMinute = YYDateUtils.getIntervalMinute(lastDate, now);
			if (intervalMinute < 30) {
				return BaseResult.fail("20001", "每30分钟可以点击一次");
			}
		}
		lastDate = now;
		String ipAddress = RequestIPUtils.getIPAddress(request);
		int totalCount = RandomUtils.nextInt(200,1000);

		new Thread(() -> {
			try {
				List<Long> ids = Lists.newArrayList();
				for (int i = 0; i < totalCount; i++) {
					int nextInt = RandomUtils.nextInt(0, 7);
					System.out.println("========" + nextInt);

					if (nextInt == 0) {
						// 创建job
						Long create = case1Service.createTrue(request, ipAddress);
						ids.add(create);
					} else if (nextInt == 1) {
						// 创建job
						Long create = case1Service.createFalse(request, ipAddress);
						ids.add(create);
					} else if (nextInt == 2) {
						// 创建job
						Long create = case1Service.createExpected(request, ipAddress);
						ids.add(create);
					} else if (nextInt == 3) {
						// 创建job
						Long create = case3Service.createLocal(request, ipAddress);
						ids.add(create);
					} else if (nextInt == 4) {
						// 创建job
						Long create = case3Service.createPriority(request, ipAddress);
						ids.add(create);
					} else if (nextInt == 5) {
						// 创建job
						Long create = case2Service.createTrue(request, ipAddress);
						ids.add(create);
					} else if (nextInt == 6) {
						// 创建job
						Long create = case2Service.createFalse(request, ipAddress);
						ids.add(create);
					}
				}
			} catch (Exception ex) {
				logger.error("create job wrong", ex);
			}
		}).start();

		return BaseResult.success("异步生成，请在yy-job控制台刷新查看任务执行情况");
	}

}
